package gui;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
//import javax.swing.JMenu;
//import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTable;

import com.googlecode.javacv.cpp.opencv_core.IplImage;

public class BVView implements ActionListener, BvViewI {

	private ArrayList<JMenuItem> menuItems = new ArrayList<JMenuItem>();
	private ArrayList<JButton> buttons = new ArrayList<JButton>();
	
	private ImagePanel originalImagePanel;
	private ImagePanel resultImagePanel;
	private TablePanel resultDataPanel;
	private IplImage originalImage;
	private IplImage resultImage;
	private JTable resultData;
	
	
	public JFrame mainFrame;
	
	public JFrame getMainFrame() {
		return mainFrame;
	}
	public IplImage getResultImage() {
		return resultImage;
	}
	public void setResultImage(IplImage resultImage) {
		this.resultImage = resultImage;
	}
	public IplImage getOriginalImage() {
		return originalImage;
	}
	
	public void setButtonEnable(String command, boolean enable) {
		// find the button with the command
		JButton toFind = null;
		for ( int i=0; i < buttons.size(); i++ ) {
			if ( command.equals(buttons.get(i).getActionCommand()) ) {
				toFind = buttons.get(i);
				break;
			}
		}
		toFind.setEnabled(enable);
	}
	public void setOriginalImage(IplImage image) {
		this.originalImage = image;
	}
	
	public BVView() {
		createAndShowGUI();
	}

	private void createAndShowGUI() {
		// Create and set up the frame.
		// The string passed as an argument will be displayed
		// as the title.

		JFrame mainFrame = new JFrame("Ball Void Detector");
		this.mainFrame = mainFrame;
		
		// Display the window.
		mainFrame.setVisible(true);
		mainFrame.setBounds(40, 40, 100, 100);
		mainFrame.setContentPane(createContentPane());
		mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		mainFrame.setBackground(new Color(255, 255, 255));
		//mainFrame.setJMenuBar(createMenuBar());

		mainFrame.pack();

	}


	// TODO : make a border with title
	private TablePanel createResultPanel() {
		TablePanel resultPane = new TablePanel();
		return resultPane;
	}

	// TODO : make a border with title
	private JPanel createControlPanel(int width, int height) {
		JPanel controlPanel = new JPanel();
		controlPanel.setPreferredSize(new Dimension(width, height));

		//Border blackline = BorderFactory.createLineBorder(Color.black);
		//controlPanel.setBorder(blackline);
		
		controlPanel.add(createButton("Load Image", LOAD_IMAGE, "Load an image to process", true));
		controlPanel.add(createButton("Process", PROCESS, "Detect balls and voids", false));
		controlPanel.add(createButton("Save Image", SAVE_IMAGE, "Save the result image", false));
		controlPanel.add(createButton("Export Data", EXPORT_DATA, "Export the result data", false));
		controlPanel.add(createButton("Exit", EXIT, "Exit", true));
		
		buttonActionListener(this);
		return controlPanel;
	}

	// TODO : make a border with title
	// TODO : convert to scroll pane and synchronize
	private JPanel createContentPane() {
		JPanel totalGUI = new JPanel();
		totalGUI.setLayout(new BorderLayout());
//		totalGUI.setSize(CONTENT_PANE_WIDTH, CONTENT_PANE_HEIGHT);

		this.originalImagePanel = new ImagePanel("Original");
		this.resultImagePanel = new ImagePanel("Result");
		this.resultDataPanel = createResultPanel();
		
		JPanel controlPanel = createControlPanel(CONTROL_PANE_WIDTH, CONTROL_PANE_HEIGHT);
		totalGUI.add(controlPanel, BorderLayout.PAGE_START);
		totalGUI.add(originalImagePanel, BorderLayout.LINE_START);
		totalGUI.add(resultImagePanel, BorderLayout.CENTER);
		totalGUI.add(resultDataPanel, BorderLayout.LINE_END);

		return totalGUI;
	}
	private void syncScrollPanes(JScrollPane scrollPane1, JScrollPane scrollPane2) {
		JScrollBar vScrollBar1 = scrollPane1.getVerticalScrollBar();
		JScrollBar hScrollBar1 = scrollPane1.getHorizontalScrollBar();
		JScrollBar vScrollBar2 = scrollPane2.getVerticalScrollBar();
		JScrollBar hScrollBar2 = scrollPane2.getHorizontalScrollBar();
		
		vScrollBar2.setModel(vScrollBar1.getModel());
		hScrollBar2.setModel(hScrollBar1.getModel());
	}
	
	// create new button and add it into the ArrayList [buttons].
	// and return the button created
	private JButton createButton(String title, String command, String toolTip, boolean enable) {
		JButton newButton = new JButton(title);
		newButton.setActionCommand(command);
		newButton.setEnabled(enable);
		newButton.setToolTipText(toolTip);
		buttons.add(newButton);
		return newButton;
	}
	// create new menu item and add it into the ArrayList [menuItems]
	// and return the menu item created
	/*
	private JMenuItem createMenuItem(String title, String command) {
		JMenuItem newItem = new JMenuItem(title);
		newItem.setActionCommand(command);
		menuItems.add(newItem);
		return newItem;
	}
	*/
	// create a new menu bar and return the menu bar created
	/*
	private JMenuBar createMenuBar() {
		JMenuBar menuBar = new JMenuBar();
		JMenu menu1 = new JMenu("File");
		menuBar.add(menu1);
				
		menu1.add(createMenuItem("Load Image", LOAD_IMAGE));
		menu1.addSeparator();
		menu1.add(createMenuItem("Save Image As", SAVE_IMAGE));
		menu1.add(createMenuItem("Export Data", EXPORT_DATA));
		menu1.addSeparator();
		menu1.add(createMenuItem("Exit", EXIT));

		JMenu menu2 = new JMenu("Help");
		menu2.add(createMenuItem("About", ABOUT));
		menu2.addSeparator();
		menu2.add(createMenuItem("Manual Page", MANUAL_PAGE));

		menuBar.add(menu2);
		
		menuActionListener(this);
		return menuBar;
	}
	*/
	// Add an action listener to the menu bar
	public void menuActionListener(ActionListener listener) {
		// add ActionListener to all the JMenuItems in the menuItems
		for (int i = 0; i < menuItems.size(); i++ ) {
			(menuItems.get(i)).addActionListener(listener);
		}
	}
	// Add an action listener to the buttons
	public void buttonActionListener(ActionListener listener) {
		// add ActionListener to all the JButtons in the buttons
		for (int i = 0; i < buttons.size(); i++ ) {
			(buttons.get(i)).addActionListener(listener);
		}
	}

	// TODO view about
	public void viewAbout() {
	}

	// TODO view manual
	public void viewManual() {
		JFrame frame = new JFrame();
		frame.setTitle("Manual");
	    
	    JTextArea box = new JTextArea();
	    box.setText("잘 못찾는 그림 제보받아요 010-2927-3077");
	      frame.getContentPane().add(box);
	      //frame.getContentPane().add(button);
	      frame.setSize(300, 300);
	      frame.setVisible(true);
	}

	// Event handling for each action
	@Override
	public void actionPerformed(ActionEvent event) {
		String command = event.getActionCommand();
		if (command.equals(LOAD_IMAGE)) {
			try {
				updateOriginalImagePanel();
			} catch (NullPointerException e) {
				;
			}
			
		}
		else if (command.equals(PROCESS)) {
			try {
				syncScrollPanes(originalImagePanel.getScrollPane(), resultImagePanel.getScrollPane());
				updateResultImagePanel();
				updateResultDataPanel();
			} catch (NullPointerException e) {
				;
			}
		}
		else if (command.equals(SAVE_IMAGE)) {
			;
		}
	}

	// Update result image
	public void updateResultImagePanel() {
		resultImagePanel.updateImage(resultImage);
	}
	
	// Update original image
	public void updateOriginalImagePanel() {
		originalImagePanel.updateImage(originalImage);
	}
	public void updateResultDataPanel() {
		resultDataPanel.updateTable(resultData);
	}
	public void setResultData(JTable resultData) {
		this.resultData = resultData;
	}
	public void setTitle(String title) {
		this.mainFrame.setTitle("Ball Void - " + title);
	}
}